package com.fr.chicken.manager.activation;

import com.fr.chicken.dao.*;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.manager.message.MessageManager;
import com.fr.chicken.manager.playerinfo.PlayerInfoManager;
import com.fr.chicken.manager.vip.VipManager;
import com.fr.chicken.model.*;
import com.fr.chicken.model.vo.ChickenInfoVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class ActivationManager {
    @Autowired
    private ActivationLogDao activationLogDao;
    @Autowired
    private PlayerInfoManager playerInfoManager;
    @Autowired
    private VipManager vipManager;
    @Autowired
    private MessageManager messageManager;
    @Autowired
    private MaxConfigureDao maxConfigureDao;
    @Autowired
    private GoodsLogDao goodsLogDao;
    @Autowired
    private RebateLogDao rebateLogDao;
    @Autowired
    private ConfigManager configManager;
    public boolean activationLogic(String activationCode,long playerId){
        GoodsLog referrer = goodsLogDao.selectByInfoAndGoodsId(activationCode,1);//49 vip 2
        PlayerInfo playerInfo = playerInfoManager.findPlayerInfoById(referrer.getPlayerId());
        ActivationLog activationLog = activationLogDao.selectByInviteeId(referrer.getPlayerId());
        assert referrer != null;
        if (playerInfo.getAgent()==1){
            if (!addActivationLog(referrer.getPlayerId(),playerId,playerInfo.getId())) {
                throw new RuntimeException("激活码注册失败");
            }
        }else{
            if (activationLog==null){
                if (!addActivationLog(referrer.getPlayerId(),playerId,0)) {
                    throw new RuntimeException("激活码注册失败");
                }
            }else {
                if (!addActivationLog(referrer.getPlayerId(),playerId,activationLog.getOneAgentId())) {
                    throw new RuntimeException("激活码注册失败");
                }
            }

        }
        int num = goodsLogDao.deleteByPrimaryKey(referrer.getId());
        if (num<1){
            throw new RuntimeException("删除激活码失败");
        }
        //返利
        RebateLog rebateLog = new RebateLog();
        int eggNums1 = configManager.getIntConfig(8);
        if (eggNums1>0) {
            rebateLog.setNum(eggNums1);
            rebateLog.setPlayerId(playerId);
            rebateLog.setRecipientId(referrer.getPlayerId());
            rebateLog.setTime(System.currentTimeMillis() / 1000);
            int num1 = rebateLogDao.insertSelective(rebateLog);
            if (num1 < 1) {
                throw new RuntimeException("返利失败");
            }
            playerInfo.setEgg(playerInfo.getEgg() + rebateLog.getNum());
            if (!playerInfoManager.savePlayerInfo(playerInfo)) {
                throw new RuntimeException("返利失败");
            }
            if (!messageManager.sendSystemMessage(referrer.getPlayerId(), eggNums1, "推荐注册返利,赠送" + eggNums1 + "个鸡蛋")) {
                throw new RuntimeException("返利失败");
            }
        }
        int eggNums2 = configManager.getIntConfig(9);
        if (eggNums2>0) {
            if (activationLog != null) {
                rebateLog.setNum(eggNums2);
                rebateLog.setPlayerId(referrer.getPlayerId());
                rebateLog.setRecipientId(activationLog.getInviterId());
                rebateLog.setTime(System.currentTimeMillis() / 1000);
                int num2 = rebateLogDao.insertSelective(rebateLog);
                if (num2 < 1) {
                    throw new RuntimeException("返利失败");
                }
                PlayerInfo playerInfo1 = playerInfoManager.findPlayerInfoById(activationLog.getInviterId());
                playerInfo1.setEgg(playerInfo1.getEgg() + rebateLog.getNum());
                if (!playerInfoManager.savePlayerInfo(playerInfo1)) {
                    throw new RuntimeException("返利失败");
                }
                if (!messageManager.sendSystemMessage(activationLog.getInviterId(), eggNums2, "推荐注册返利,赠送" + eggNums2 + "个鸡蛋")) {
                    throw new RuntimeException("返利失败");
                }
            }
        }
        //vip升级
        vipManager.updataVip(referrer.getPlayerId(),countActivationLog(referrer.getPlayerId()));
        registerAward(referrer.getPlayerId());
        return true;

    }

    public int countActivationLog(long playerId) {
        return activationLogDao.countByInviterId(playerId);
    }

    /**
     * 添加激活记录
     * */
    private boolean addActivationLog(long inviterId,long inviteeId,long oneAgentId){
        ActivationLog activationLog = new ActivationLog();
        activationLog.setInviterId(inviterId);
        activationLog.setInviteeId(inviteeId);
        activationLog.setOneAgentId(oneAgentId);
        return activationLogDao.insertSelective(activationLog) >= 1;
    }
    public String generateActivation(){
        String random = RandomStringUtils.randomAlphanumeric(6);
        GoodsLog goodsLog = goodsLogDao.selectByInfoAndGoodsId(random,1);
        if (goodsLog==null){
            return random;
        }
        return generateActivation();
    }
    /**
     * 发送注册奖励
     * */
    public void registerAward(Long playerId) {
        PlayerInfo referrer = playerInfoManager.findPlayerInfoById(playerId);
        int egg = configManager.getIntConfig(6);;
        if (egg>0) {
            if (!playerInfoManager.addEgg(referrer, egg)) {
                throw new RuntimeException("赠送上家鸡蛋失败");
            }
            if (!messageManager.sendSystemMessage(referrer.getId(), egg, "推荐注册有礼,赠送" + egg + "个鸡蛋")) {
                throw new RuntimeException("添加赠送记录失败");
            }
        }
    }
}
